﻿using ProjectFootball.MVC.Foundation;
using ProjectFootball.MVC.Foundation.ResourceLibrary;
using ProjectFootball.MVC.Models;
using ProjectFootball.MVC.Models.EntityFramework.Entities;
using ProjectFootball.MVC.Models.RepositoryProviders;
using System.Linq;
using System.Web.Mvc;

namespace ProjectFootball.MVC.Controllers
{
    public class HomeController : BaseController
    {
        #region Constructor
        public HomeController(IRepositoryProvider provider)
            :base(provider)
        {
        }
        #endregion

        public ActionResult Index(string id)
        {
            if (string.IsNullOrEmpty(id) || id.Length < 3)
                return View(new CombinedSearchViewModel());

            CombinedSearchViewModel combinedSearchViewModel = new CombinedSearchViewModel()
            {
                Competitions = Provider.CompetitionRepository.Where(c => c.CompetitionName.Contains(id)).Search(User, SearchType.Text),
                Countries = Provider.CountryRepository.Where(c => c.CountryName.Contains(id)).Search(User, SearchType.Text),
                People = Provider.PersonRepository.Where(c => (c.Forenames + " " + c.Surname).Contains(id)).Search(User, SearchType.Text),
                TeamVersions = Provider.TeamVersionRepository.Where(t => t.TeamName.Contains(id)).Search(User, SearchType.Text),
                Venues = Provider.VenueRepository.Where(t => t.VenueName.Contains(id)).Search(User, SearchType.Text)
            };

            if (combinedSearchViewModel.Count == 1)
            {
                if (combinedSearchViewModel.Competitions.Any())
                    return RedirectToAction("Index", "Details", new { area = "CompetitionArea", id = combinedSearchViewModel.Competitions.FirstOrDefault().Competition.Pk });

                if (combinedSearchViewModel.Countries.Any())
                    return RedirectToAction("Index", "Details", new { area = "CountryArea", id = combinedSearchViewModel.Countries.FirstOrDefault().Country.Pk });

                if (combinedSearchViewModel.People.Any())
                    return RedirectToAction("Index", "Details", new { area = "PersonArea", id = combinedSearchViewModel.People.FirstOrDefault().Person.Pk });

                if (combinedSearchViewModel.TeamVersions.Any())
                    return RedirectToAction("Index", "Details", new { area = "TeamArea", id = combinedSearchViewModel.TeamVersions.FirstOrDefault().TeamVersion.Pk });

                if (combinedSearchViewModel.Venues.Any())
                    return RedirectToAction("Index", "Details", new { area = "VenueArea", id = combinedSearchViewModel.Venues.FirstOrDefault().Venue.Pk });
            }

            return View(combinedSearchViewModel);
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your app description page.";

            return View();
        }

		public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}
